 var tigerStep3 = {
	load  : function () {
		this.eventRegister();
		 
		
		new Ext.form.DateField({       
		      applyTo : 'adStartDate',
		      format: 'Y/m/d',       
		      emptyText: '請選擇開始日期 ...' 
		});       
		  
		new Ext.form.DateField({       
		      applyTo : 'adEndDate',       
		      format: 'Y/m/d',
		      emptyText: '請選擇結束日期 ...'        
		}); 	
		this.setFormValue();
	},
	
	eventRegister : function () {
		Ext.get("btnPrevious").on("click", this.previous);
		Ext.get("btnNext").on("click", this.next);
	},
	
	
	setFormValue : function () {
		if (ad != null) {
			if (ad.adName) {
				Ext.getDom("adName").value = ad.adName;
			}
			
			if (ad.budget) {
				Ext.getDom("budget").value = ad.budget;
			}
			
			if (ad.adBudgetType) {
				Ext.getDom("adBudgetType").value = ad.adBudgetType;
			}
			 
			if (ad.adStartDate) {
				Ext.getDom("adStartDate").value = ad.adStartDate.replaceAll("-","/");
			}
			
			if (ad.adEndDate) {
				Ext.getDom("adEndDate").value = ad.adEndDate.replaceAll("-","/");;
			}
			
			if (ad.adBillingType) {
				document.getElementsByName("adBillingType")[ad.adBillingType == "CMP" ? 0 :1].checked = true;
			}
		}
	},
	
	previous : function () {
		window.location ="/ad/html/frontoffice/tigerStep2.php";
	},
	
	next : function () {
		 if (Ext.getDom("adName").value=="") {
		 	alert("請輸入[宣傳活動名稱]!");
		 	Ext.getDom("adName").focus();
		 	return ;
		 }
		 
		 if (isNaN(Ext.getDom("budget").value)) {
		 	alert("[預算]請輸入數字!");
		 	Ext.getDom("budget").focus();
		 	return ;
		 }
		 
		  if (Ext.getDom("budget").value<= 0) {
		 	alert("[預算]需大於0!!");
		 	Ext.getDom("budget").focus();
		 	return ;
		 }
		 
		 if (Ext.getDom("adStartDate").value == "請選擇開始日期 ...") {
		 	 alert("請輸入[開始 刊登日期]日期!")
		 	 return;
		 }
		 
		 if (!Ext.getDom("adStartDate").value.isDate()) {
		 	 alert("[開始 刊登日期] 請輸入正確的日期格式!")
		 	 return;
		 }
		 
		 var diff = new Date(new Date().string()) - new Date(Ext.getDom("adStartDate").value);
 
		 diff = Math.abs(diff);
 
		 diff = diff / 1000 / 60 / 60 / 24; // 由亳秒轉成天
 
		 if (diff <=2 ) {
			 alert("[開始 刊登日期] 需大於現在時間 兩天!")
		 	 return;
		 	
		 }
		 
		 
		  if (Ext.getDom("adEndDate").value == "請選擇結束日期 ...") {
		 	 alert("請輸入[結束 刊登日期]日期!")
		 	 return;
		 }
		 
		 if (!Ext.getDom("adEndDate").value.isDate()) {
		 	 alert("[結束 刊登日期] 請輸入正確的日期格式!")
		 	 return;
		 }
		 
		 if (Ext.getDom("adStartDate").value > Ext.getDom("adEndDate").value) {
		 	alert("[刊登日期] 區間錯誤!");
		 	return;
		 }
		 
		 Ext.getDom("form").submit();
	}
};

window.onload = function () {
	tigerStep3.load();	
}